1 package attr;
2
3 import java.lang.*;
4 import java.sql.*;
5 import javax.swing.*;
6 import javax.swing.table.*;
7 import attr.*;
8 import activity.*;

9
10 public
class Employee extends User {
11     
private String employeeName;
12     
private String phoneNumber;
13     
private String role;
14     
public static String[] columnNames = {"EmployeeID", "EmployeeName", "PhoneNumber", "Role", "Salary"};
15     
public static String[] roles = {"General", "Manager"};
16     
private double salary;
17     
public Employee(String userId) {
18         super(userId);
19         
this.setStatus(0);
20     }
21     
22     
public void setEmployeeName(String name) {
23         
if (!name.isEmpty())
24             
this.employeeName = name;
25         
else
26             
throw new IllegalArgumentException("Fill in the name");
27     }
28     
public void setPhoneNumber(int num) {
29         
this.phoneNumber = "+880"+num;
30     }
31     
public void setRole(String role) {
32         
this.role = role;
33     }
34     
public void setSalary(double salary) {
35         
this.salary = salary;
36     }
37     
public String getPhoneNumber() {
38         
return phoneNumber;
39     }
40     
public String getEmployeeName() {
41         
return employeeName;
42     }
43     
public String getRole() {
44         
return role;
45     }
46     
public double getSalary() {
47         
return salary;
48     }
49     
50     
public void createEmployee() {
51         String query1 =
"INSERT INTO `login` VALUES ('"+userId+"','"+password+"',"+status+");";
52         String query2 =
"INSERT INTO `employee` VALUES ('"+userId+"','"+employeeName+"','"+phoneNumber+"','"+role+"', '"+salary+"');";
53         Connection con =
null;
54         Statement st =
null;
55         System.
out.println(query1);
56         System.
out.println(query2);
57         
try {
58             Class.forName(
"com.mysql.jdbc.Driver");
59             System.
out.println("driver loaded");
60             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
61             System.
out.println("connection done");//connection with database established
62             st = con.createStatement();
//create statement
63             System.
out.println("statement created");
64             st.execute(query1);
//insert
65             st.execute(query2);
66             System.
out.println("data inserted");
67             JOptionPane.showMessageDialog(
null,"Account Created!");
68         }
69         
catch(Exception ex) {
70             JOptionPane.showMessageDialog(
null,"Failed to create account!");
71             System.
out.println("Exception : " +ex.getMessage());
72         }
73         
finally {
74             
try {
75                 
if(st!=null)
76                     st.close();
77
78                 
if(con!=null)
79                     con.close();
80             }
81             
catch(Exception ex) {}
82         }
83     }
84     
85     
public void fetch() {
86         String query =
"SELECT `userId`, `employeeName`, `phoneNumber`, `role`, `salary` FROM `employee` WHERE userId='"+this.userId+"';";
87         Connection con =
null;
88         Statement st =
null;
89         ResultSet rs =
null;
90         System.
out.println(query);
91         
try {
92             Class.forName(
"com.mysql.jdbc.Driver");
93             System.
out.println("driver loaded");
94             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
95             System.
out.println("connection done");//connection with database established
96             st = con.createStatement();
//create statement
97             System.
out.println("statement created");
98             rs = st.executeQuery(query);
//getting result
99             System.
out.println("results received");
100             boolean flag =
false;
101             
while(rs.next()) {
102                 
this.employeeName = rs.getString("employeeName");
103                 
this.phoneNumber = rs.getString("phoneNumber");
104                 
this.role = rs.getString("role");
105                 
this.salary = rs.getDouble("salary");
106             }
107         }
108         
catch(Exception ex) {
109             System.
out.println("Exception : " +ex.getMessage());
110         }
111         
finally {
112             
try {
113                 
if(rs!=null)
114                     rs.close();
115
116                 
if(st!=null)
117                     st.close();
118
119                 
if(con!=null)
120                     con.close();
121             }
122             
catch(Exception ex) {}
123         }
124     }
125     
126     
public void updateEmployee(String name, int phone, String role, double salary) {
127         String query =
"UPDATE `employee` SET `employeeName`='"+name+"', `phoneNumber`='+880"+phone+"', `role`='"+role+"', `salary`="+salary+" WHERE `userId`='"+this.userId+"';";
128         Connection con =
null;
129         Statement st =
null;
130         System.
out.println(query);
131         
try {
132             Class.forName(
"com.mysql.jdbc.Driver");
133             System.
out.println("driver loaded");
134             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
135             System.
out.println("connection done");//connection with database established
136             st = con.createStatement();
//create statement
137             System.
out.println("statement created");
138             st.executeUpdate(query);
//insert
139             System.
out.println("data inserted");
140             JOptionPane.showMessageDialog(
null,"Information Updated!");
141             
this.employeeName = name;
142             
this.phoneNumber = "+880"+phone;
143             
this.role = role;
144             
this.salary = salary;
145         }
146         
catch(Exception ex) {
147             JOptionPane.showMessageDialog(
null,"Failed to update account!");
148             System.
out.println("Exception : " +ex.getMessage());
149         }
150         
finally {
151             
try {
152                 
if(st!=null)
153                     st.close();
154
155                 
if(con!=null)
156                     con.close();
157             }
158             
catch(Exception ex) {}
159         }
160     }
161     
162     
public void deleteEmployee() {
163         String query1 =
"DELETE FROM `login` WHERE `userId`='"+this.userId+"';";
164         String query2 =
"DELETE FROM `employee` WHERE `userId`='"+this.userId+"';";
165         Connection con =
null;
166         Statement st =
null;
167         System.
out.println(query1);
168         System.
out.println(query2);
169         
try {
170             Class.forName(
"com.mysql.jdbc.Driver");
171             System.
out.println("driver loaded");
172             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
173             System.
out.println("connection done");//connection with database established
174             st = con.createStatement();
//create statement
175             System.
out.println("statement created");
176             st.execute(query1);
177             st.execute(query2);
//delete
178             System.
out.println("data deleted");
179             JOptionPane.showMessageDialog(
null,"Account Deleted!");
180             
this.userId = "";
181             
this.employeeName = "";
182             
this.phoneNumber = "";
183             
this.role = "";
184             
this.salary = 0;
185         }
186         
catch(Exception ex) {
187             JOptionPane.showMessageDialog(
null,"Failed to delete account!");
188             System.
out.println("Exception : " +ex.getMessage());
189         }
190         
finally {
191             
try {
192                 
if(st!=null)
193                     st.close();
194
195                 
if(con!=null)
196                     con.close();
197             }
198             
catch(Exception ex) {}
199         }
200     }
201     
202     
public static DefaultTableModel searchEmployee(String keyword, String byWhat) {
203         DefaultTableModel model =
new DefaultTableModel();
204         model.setColumnIdentifiers(columnNames);
205         String query =
"SELECT * FROM `employee` WHERE `userId`='"+keyword+"';";
206         
if (byWhat.equals("By Name"))
207             query =
"SELECT * FROM `employee` WHERE `employeeName` LIKE '%"+keyword+"%';";
208         
else {}
209         Connection con =
null;
210         Statement st =
null;
211         ResultSet rs =
null;
212         System.
out.println(query);
213         
try {
214             Class.forName(
"com.mysql.jdbc.Driver");
215             System.
out.println("driver loaded");
216             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
217             System.
out.println("connection done");//connection with database established
218             st = con.createStatement();
//create statement
219             System.
out.println("statement created");
220             rs = st.executeQuery(query);
//getting result
221             System.
out.println("results received");
222             
223             
while(rs.next()) {
224                 model.addRow(
new Object[]{rs.getString("userId"), rs.getString("employeeName"), rs.getString("phoneNumber"), rs.getString("role"), rs.getString("salary")});
225             }
226         }
227         
catch(Exception ex) {
228             System.
out.println("Exception : " +ex.getMessage());
229         }
230         
finally {
231             
try {
232                 
if(rs!=null)
233                     rs.close();
234
235                 
if(st!=null)
236                     st.close();
237
238                 
if(con!=null)
239                     con.close();
240             }
241             
catch(Exception ex) {}
242         }
243         
return model;
244     }
245 }


Gõ tìm kiếm nhanh...